/*
 * @Author: lin rq
 * @Date: 2024-02-28 14:59:12
 * @LastEditors: lrq 1162574925@qq.com
 * @LastEditTime: 2024-02-28 16:06:51
 * @FilePath: \my-project\src\common\index.js
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
 */
import Storage from "@/utils/storage";
import { weiXinLoginApi } from "@/servers";

// 检查是否登录
const judgeLogin = () => {
  const userInfo = Storage.getCache("userInfo");
  const userId = Storage.getCache("userId");
  if (!userId || !userInfo) {
    uni.showToast({
      title: "请先登录",
      icon: "none",
    });
    uni.switchTab({
      url: "/pages/mine/mine",
    });
    return false;
  }
  return true;
};

/**
 * 将图片保存到本地相册
 * @param {图片路径} path
 */
const savePhotoAlbum = (path) => {
  uni.downloadFile({
    url: path,
    success: (res) => {
      if (res.statusCode === 200) {
        uni.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: () => {
            uni.showToast({ title: "保存成功" });
          },
          fail: (fail) => {
            console.log(fail, "失败");
          },
          complete: (complete) => {
            console.log(complete, "执行");
          },
        });
      }
    },
  });
};

// 一键检测微信登陆
const Wxlogin = () => {
  uni.login({
    force: true,
    success: async (res) => {
      const code = res.code;
      Storage.setStorage("userInfo", code);
      const apiRes = await weiXinLoginApi(code, "wx31f89360ca7a11d0");
      Storage.setStorage("userId", apiRes.data.user_id);
    },
    fail(res) {
      console.log(`login 调用失败`);
    },
    complete(res) {
      console.log("login 调用完成");
    },
  });
};

/**
 * 预览图片
 * @param {图片数据} urls
 * @param {默认索引} current
 */
const previewImgFn = (urls, current) => {
  uni.previewImage({
    urls,
    current,
    success: (success) => {
      console.log(success, "成功");
    },
    fail: (fail) => {
      console.log(fail, "失败");
    },
    complete: (complete) => {
      console.log(complete, "执行");
    },
  });
};

const UPLOAD_HOST = "***"; // 真实项目远程地址
/**
 * 上传图片的远程服务器
 * @param {图片路径} filePath
 * @param {vue实例} that
 */
const uploadFn = (filePath, that) => {
  uni.uploadFile({
    url: UPLOAD_HOST,
    filePath,
    name: "file",
    header: {
      "content-type": "application/json; charset=utf-8",
    },
    // #ifdef MP-TOUTIAO
    formData: {
      appid: "tt015577e89e6485d601",
    },
    // #endif
    success: (res) => {
      const dataObject = JSON.parse(res?.data);
      // #ifdef MP-TOUTIAO
      if (dataObject.data.check_img_result.err_no !== 0) {
        that.creactCartoonImg = "";
        uni.showToast({ title: "图片违规！！", icon: "fail" });
        return;
      }
      // #endif
      const { url } = dataObject.data;
      that.isUpload = false;
      that.creactCartoonImg = url;
      that.noticeText = `上传成功，点击生成头像即可开始制作。`;
      uni.showToast({
        title: "上传成功",
        icon: "success",
      });
    },
    fail: function () {
      uni.showToast({
        icon: "fail",
        title: "上传失败",
      });
    },
    complete: (res) => {},
  });
};

/**
 * @description {内容} {是否显示蒙层} {持续时长} {提示图标}
 * @param {内容} title
 * @param {是否显示蒙层} mask
 * @param {持续时长} duration
 * @param {提示图标} icon
 * @returns
 */
const toast = ({title, mask = false, duration = 2000, icon = "success"}) =>
  uni.showToast({
    title: title,
    mask,
    duration,
    icon,
    success: () => {
      console.log("toast展示");
    },
  });



/**
 * @description:  {标题} {内容} {是否展示取消}  {取消按钮文字} {取消按钮颜色} {确定按钮文字} {确定按钮颜色} 
 * @param {标题} title
 * @param {内容} content
 * @param {是否展示取消} showCancel
 * @param {取消按钮文字} cancelText
 * @param {取消按钮颜色} cancelColor
 * @param {确定按钮文字} confirmText
 * @param {确定按钮颜色} confirmColor
 * @return {*}
 */
const modal = ({
  title = "温馨提示",
  content,
  showCancel = true,
  cancelText = "取消",
  cancelColor = "#000",
  confirmText = "确定",
  confirmColor = "#007acc",
}) =>
  uni.showModal({
    title: title,
    content,
    showCancel,
    cancelText,
    cancelColor,
    confirmText,
    confirmColor,
    success: () => {
      console.log("modal 展示");
    },
  });

export {
  judgeLogin,
  savePhotoAlbum,
  Wxlogin,
  previewImgFn,
  uploadFn,
  toast,
  modal,
};
